package whu.cn.whushare.ui.main.known.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.ImageWorkerManager;
import whu.cn.whushare.util.Utils;

public class SearchUserAdapter extends BaseAdapter {
	private List<User> mUsers;
	private Context mContext;

	public SearchUserAdapter(List<User> mUsers, Context mContext) {
		this.mUsers = mUsers;
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		return mUsers.size();
	}

	@Override
	public User getItem(int position) {
		return mUsers.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		User user = getItem(position);
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_search_user_item, null);
			viewHolder = new ViewHolder();
			ViewMappingUtil.mapView(viewHolder, convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ImageWorkerManager.getInstance(mContext).cancelRequest(viewHolder.avatar);
		ImageWorkerManager.getInstance(mContext)
				.loadCommonImage(Utils.getPhotoUrl(user.getAvatar()), viewHolder.avatar);
		viewHolder.name.setText(user.getNickName());
		viewHolder.introduce.setText(user.getIntroduce());

		return convertView;
	}

	private class ViewHolder {
		@ViewMapping(id = R.id.avatar)
		public ImageView avatar;
		@ViewMapping(id = R.id.user_name)
		public TextView name;
		@ViewMapping(id = R.id.introduce)
		public TextView introduce;
	}
}
